var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
        kuaiyinGoodsAttributeDialogVisible: false,
        kuaiyinCraftSizeDialogVisible: false,
        stepPriceDialogVisible: false,
		title: null,
        kuaiyinGoodsCraftAttributeData: [],// 印刷商品属性table
        kuaiyinGoodsCraftData:[],// 印刷商品工艺
        kuaiyinCraftOptionData:[],// 印刷商品工艺选项table
        stepPriceData:[],// 阶梯价格table
        stepPriceRow: null, //当前阶梯价所在option 行
        craftSizeOption:[{value: '[210,297]',label: 'A4(210mm*297mm)'},{value: '[297,440]',label: 'A3(297mm*440mm)'},
                          {value: '0',label: '自定义'},{value: null,label: '不设置'}],
        craftSizeRow: null, //当前行尺寸
        craftSizeLength: null, // 工艺长
        craftSizeWidth: null, // 工艺宽
        showCustom: false, //自定义尺寸
	},

	mounted: function () {
		this.initData();
    },

	updated: function () {
        //将已经存在的赋值选中
        for (var i = 0; i < vm.kuaiyinCraftOptionData.length; i++) {
            var temp = vm.kuaiyinCraftOptionData[i];
            if (temp.craftPriceId) {
                this.$refs.kuaiyinCraftOptionTable.toggleRowSelection(temp, true);
            }
        }
    },

	methods: {
		/*页面初始化数据*/
		initData : function () {
			// 请求已经存在的 商品工艺价格
            $.get(baseURL + "goodsCraftPrice/quoteGoodsCraftPrice/getKuaiyinGosodsCraftPriceList", function(r){
                vm.kuaiyinGoodsCraftAttributeData = r.kuaiyinGoodsCraftPriceData.kuaiyinGoodsCraftList;

                //重新赋值stepPrice
                if (r.kuaiyinGoodsCraftPriceData.kuaiyinGoodsCraftPriceList){
                    for(var i=0; i<r.kuaiyinGoodsCraftPriceData.kuaiyinGoodsCraftPriceList.length; i++){
                        var temp =  r.kuaiyinGoodsCraftPriceData.kuaiyinGoodsCraftPriceList[i];
                        if (temp.stepPrice){
                            temp.stepPrice = JSON.parse(temp.stepPrice);
                        }
                    }
                }
                vm.kuaiyinCraftOptionData = r.kuaiyinGoodsCraftPriceData.kuaiyinGoodsCraftPriceList;

            });
        },

		// 添加商品工艺
        addGoodsCraft: function () {
        	// 查询商品工艺
            $.get(baseURL + "goodsCraftPrice/quoteGoodsCraftPrice/getQuoteGoodsCraftList?attributeOrigin=fast", function(r){
                vm.kuaiyinGoodsCraftData = r.goodsCraftList;
                vm.kuaiyinGoodsAttributeDialogVisible = true;
            });

        },

		// 添加当前行到左侧 基础商品属性表
        addToLeftTable: function (index) {
        	var temp =  vm.kuaiyinGoodsCraftData[index];
            var selected =  vm.kuaiyinGoodsCraftAttributeData.find(function (option) {
				return option.attributeId === temp.attributeId;
            })
			if (selected){
                this.$message.error('该工艺已经添加 !');
            	return;
			}
            this.$refs.kuaiyinGoodsCraftTable.toggleRowSelection(temp,true);
            vm.kuaiyinGoodsCraftAttributeData.push(temp);

        },

		// 添加选中的到当前行
        addSelectedToLeftTable: function () {
            if (this.$refs.kuaiyinGoodsCraftTable.selection.length == 0){
                this.$message.error('至少选中一个工艺 !');
            	return;
			}

			for(var index in this.$refs.kuaiyinGoodsCraftTable.selection){
            	var temp = this.$refs.kuaiyinGoodsCraftTable.selection[index];
                var selected =  vm.kuaiyinGoodsCraftAttributeData.find(function (option) {
                    return option.attributeId === temp.attributeId;
                })
				if(!selected){
                    vm.kuaiyinGoodsCraftAttributeData.push(temp);
				}
			}

			vm.kuaiyinGoodsAttributeDialogVisible = false;
        },

		// 工艺当前行 点击事件
        goodsCraftAttributeClicked: function (row, event, column) {
		    var craftName = row.attributeName;
		    var self = this;
            // 查询商品工艺选项
            $.get(baseURL + "goodsCraftPrice/quoteGoodsCraftPrice/getKuaiyinGoodsCraftOptionList?attributeId="+row.attributeId, function(r){
                //重新赋值stepPrice
                if (r.code == 0){
                    if (r.kuaiyinGoodsCraftOptionPriceList.length > 0){
                        for(var i=0; i<r.kuaiyinGoodsCraftOptionPriceList.length; i++){
                            var temp =  r.kuaiyinGoodsCraftOptionPriceList[i];
                            if (temp.stepPrice){
                                temp.stepPrice = JSON.parse(temp.stepPrice);
                            }
                        }
                    }else {
                        self.$message({type: 'info', message: craftName +" 暂无选项!"});
                    }
                }else {
                    alert(r.msg);
                }

                vm.kuaiyinCraftOptionData = r.kuaiyinGoodsCraftOptionPriceList;
            });

        },

        /*设置尺寸*/
        craftSizeSetting: function (row) {
            if(row.craftSize){
                vm.craftSizeRow = row.craftSize;
            }else {
                vm.craftSizeRow = '[210,297]';// 默认a4
            }
            // 保留当前option 所在行
            vm.stepPriceRow = row;
            vm.kuaiyinCraftSizeDialogVisible = true;
        },

        /*尺寸选择变化*/
        craftSizeChange: function (value) {
            if(value === '0'){ // 自定义尺寸
                vm.showCustom = true;
            }else {
                vm.showCustom = false;
            }

        },

        //保存尺寸
        saveCraftSize : function () {
            // 取出当前行
            var row = vm.stepPriceRow;

            // 判断尺寸类型
            var craftSize;
            if(vm.craftSizeRow === '0'){ //自定义
                var craftSizeArr = [];
                var reg = /^[0-9]*^\d{1,}$/;
                if(!reg.test(vm.craftSizeLength)){
                    this.$message.error('请输入正确的长度 !');
                    return;
                }
                if(!reg.test(vm.craftSizeWidth)){
                    this.$message.error('请输入正确的宽度 !');
                    return;
                }
                craftSizeArr.push(Number(vm.craftSizeLength));
                craftSizeArr.push(Number(vm.craftSizeWidth));
                craftSize =  JSON.stringify(craftSizeArr);
            }else {
                craftSize = vm.craftSizeRow;
            }

            // 判断craftSize 是否存在
            var temp = vm.kuaiyinCraftOptionData.find(function (option) {
                if(option.optionId === row.optionId && option.craftSize === craftSize){
                    return option;
                }
            })
            if (temp){ //改分类尺寸已经存在
                this.$message.error('该分类尺寸已经存在 !');
                return;
            }

            row.craftSize = craftSize;


            // 清空临时数据
            vm.stepPriceRow = null;
            vm.kuaiyinCraftSizeDialogVisible = false;
            vm.showCustom = false;
            vm.craftSizeRow = null;
            vm.craftSizeLength = null;
            vm.craftSizeWidth = null;


        },

        /*添加一行选项*/
        addCraftOptionRow: function (row, index) {
            // 先拷贝一个对象 避免数据引用混乱
            var temp = {};
            temp.craftPriceId = null; //id 置空
            temp.optionId = row.optionId;
            temp.optionName = row.optionName;
            temp.optionValue = row.optionValue;
            temp.unitPrice = row.unitPrice;
            temp.minUnitPrice = row.minUnitPrice;
            temp.craftSize = null; // 尺寸必须手动选
            temp.minNum = row.minNum;
            temp.minBeginPrice = row.minBeginPrice;
            temp.fixedPrice = row.fixedPrice;
            temp.floatCoefficient = row.floatCoefficient;
            temp.stepPrice = row.stepPrice;

            vm.kuaiyinCraftOptionData.splice(index+1,0,temp);
        },

        /*删除一行选项*/
        deleteCraftOptionRow: function (rows, index) {
            // 判断是否存在后台报价
            var self =this;
            var row = rows[index];
            // 判断后台是否 已经存在价格
            if(row.craftPriceId){
                //存在id 则请求后台删除 此处id 仅代表存在价格 并不是该工艺的价格id
                this.$confirm('确定要删除'+ row.attributeName+"价格吗?", '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    $.ajax({
                        type: "POST",
                        url: baseURL + "goodsCraftPrice/quoteGoodsCraftPrice/deleteByIds",
                        contentType: "application/json",
                        data: JSON.stringify([row.craftPriceId]),
                        success: function(r){
                            if(r.code == 0){
                                self.$message({message: '删除成功 !' ,type: 'success'});
                                rows.splice(index, 1);
                            }else{
                                alert(r.msg);
                            }
                        }
                    });
                }).catch(() => {
                    this.$message({type: 'info', message: '已取消删除'});
                })
            }else {
                // 仅仅删除页面数据即可
                rows.splice(index, 1);
            }

        },


		/*设置阶梯价格*/
        openStepPriceDialog: function (row) {
            //先初始化数据
			if(row.stepPrice &&  row.stepPrice.length > 0){
                vm.stepPriceData = row.stepPrice;

			}else {
                vm.stepPriceData = [];
                var temp={"beginNum": null, "endNum": null, "singlePrice": null , "remark": null};
                // 加入一条空白数据
                vm.stepPriceData.push(temp);
            }

            // 保留当前option 所在行
            vm.stepPriceRow = row;
        	vm.stepPriceDialogVisible = true;
        },

		/*保存阶梯价*/
        saveStepPrice: function () {
            // 数据校验
            for(var i=0; i<vm.stepPriceData.length; i++){
                if(!vm.stepPriceData[i].beginNum  && !vm.stepPriceData[i].endNum  &&
                    !vm.stepPriceData[i].singlePrice ){
                    //去除空元素
                    vm.stepPriceData.splice(i,1);
                }
            }

			var row = vm.stepPriceRow;

            row.stepPrice = vm.stepPriceData;

            // 清空临时数据
            vm.stepPriceRow = null;
            vm.stepPriceData = [];
            vm.stepPriceDialogVisible = false;
        },

		/*添加一行阶梯价*/
        addOneStepPrice: function () {
            var temp={"beginNum": null, "endNum": null, "singlePrice": null , "remark": null};
            vm.stepPriceData.push(temp);
        },

		/*删除一行数据*/
        deleteOneStepPrice: function (index, rows) {
            if(rows.length >1){
                rows.splice(index, 1);
            }
        },

		/*一键设置价格为第一行价格*/
        oneKeySetPrice: function () {
            this.$confirm('确定要把所有选项价格一键设置为第一行价格吗', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                // 取出第一行数据
                for(var i= 0 ; i < vm.kuaiyinCraftOptionData.length ;i++){
                    // 从第二行开始 赋值

                    var temp = vm.kuaiyinCraftOptionData[i];
                    if (!temp.craftPriceId){
                        temp.craftSize = null; // 尺寸需要重置 不赋值
                        temp.unitPrice = vm.kuaiyinCraftOptionData[0].unitPrice;
                        temp.minUnitPrice = vm.kuaiyinCraftOptionData[0].minUnitPrice;
                        temp.minBeginPrice = vm.kuaiyinCraftOptionData[0].minBeginPrice;
                        temp.floatCoefficient = vm.kuaiyinCraftOptionData[0].floatCoefficient;
                        temp.stepPrice = vm.kuaiyinCraftOptionData[0].stepPrice;
                        temp.fixedPrice = vm.kuaiyinCraftOptionData[0].fixedPrice;
                        this.$refs.kuaiyinCraftOptionTable.toggleRowSelection(temp, true);
                    }else {
                        this.$refs.kuaiyinCraftOptionTable.toggleRowSelection(temp, false);
                    }

                }
                this.$message({message: '设置成功 !' ,type: 'success'});


            }).catch(() => {
                this.$message({type: 'info', message: '已取消一键设价'});
            })

        },

        //选中选项
        goodsCraftOptionSelected: function (selection, row) {
            var selected = selection.length && selection.indexOf(row) !== -1;
            if (selected) {
                // 如果是选中 则把 几个为null的价格设置为 0
                row.unitPrice = row.unitPrice == null ? 0 : row.unitPrice;
                row.minUnitPrice = row.minUnitPrice == null ? 0 : row.minUnitPrice;
                row.minBeginPrice = row.minBeginPrice == null ? 0 : row.minBeginPrice;
                row.fixedPrice = row.fixedPrice == null ? 0 : row.fixedPrice;
                row.floatCoefficient = row.floatCoefficient == null ? 0 : row.floatCoefficient;
                // 如果同时为0 给出提示
                if (row.unitPrice == 0 && row.minUnitPrice == 0 &&
                    row.minBeginPrice == 0 && row.fixedPrice == 0 &&
                    row.floatCoefficient == 0){
                    this.$message({type: 'info', message: '所勾选价格全部为0 请注意确认 !'});
                }
            }

        },

        //全选
        goodsCraftOptionSelectedAll: function (selection) {
            if (selection.length >0){//全选中
                for (var i=0; i< selection.length;i++){
                    var row = selection[i];
                    // 循环将null转为0
                    row.unitPrice = row.unitPrice == null ? 0 : row.unitPrice;
                    row.minUnitPrice = row.minUnitPrice == null ? 0 : row.minUnitPrice;
                    row.minBeginPrice = row.minBeginPrice == null ? 0 : row.minBeginPrice;
                    row.fixedPrice = row.fixedPrice == null ? 0 : row.fixedPrice;
                    row.floatCoefficient = row.floatCoefficient == null ? 0 : row.floatCoefficient;
                    // 如果同时为0 给出提示
                    if (row.unitPrice == 0 && row.minUnitPrice == 0 &&
                        row.minBeginPrice == 0 && row.fixedPrice == 0 &&
                        row.floatCoefficient == 0){
                        this.$message({type: 'info', message: row.optionName +"所在行价格都为0 请注意查看!"});
                    }
                }

            }

        },

		/*保存基础商品工艺价格*/
        saveGoodsCraftPrice: function () {
			var postData =[];
			// 先拷贝一个数据 避免页面数据错误
            var newSelectionObject = jQuery.extend(true, [], this.$refs.kuaiyinCraftOptionTable.selection);
            for(var index  in newSelectionObject){
				var currentSelection = newSelectionObject[index];
				if (currentSelection.stepPrice){
                    currentSelection.stepPrice = JSON.stringify(currentSelection.stepPrice);
                }
                postData.push(currentSelection);
			}

			if(postData.length == 0){
                this.$message.error('至少选中一个工艺选项 !');
                return;
            }
            if (postData.length > 30){
                this.$message.error('一次最多添加30个工艺选项价格 !');
                return;
            }
            var self = this;

            $.ajax({
                type: "POST",
                url: baseURL + "goodsCraftPrice/quoteGoodsCraftPrice/saveKuaiyin",
                contentType: "application/json",
                data:JSON.stringify({"craftPriceList": postData}),
                success: function(r){
                    if(r.code === 0){
                        self.$message({message: '保存成功 !' ,type: 'success'});
                    }else{
                        alert(r.msg);
                    }
                }
            });

        },

		/*根据工艺删除价格*/
        deleteCraftAttribute: function (index, rows) {

            var self =this;
        	var row = rows[index];
        	// 判断后台是否 已经存在价格
            if(row.craftPriceId){
				//存在id 则请求后台删除 此处id 仅代表存在价格 并不是该工艺的价格id
                this.$confirm('确定要删除'+ row.attributeName+"价格吗?", '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    $.ajax({
                        type: "get",
                        url: baseURL + "goodsCraftPrice/quoteGoodsCraftPrice/deleteByAttributeId?attributeId="+row.attributeId,
                        success: function(r){
                            if(r.code == 0){
                                self.$message({message: '删除成功 !' ,type: 'success'});
                                self.initData();
                            }else{
                                alert(r.msg);
                            }
                        }
                    });
                }).catch(() => {
                    this.$message({type: 'info', message: '已取消删除'});
                })
			}else {
				// 仅仅删除页面数据即可
                rows.splice(index, 1);
                vm.kuaiyinCraftOptionData = [];
			}

        },

        /*根据选中工艺价格删除*/
        deleteCraftPrice: function () {
            // 取出所有选中的工艺价格
           var selected =  this.$refs.kuaiyinCraftOptionTable.selection;
           if(selected.length ==0){
               this.$message.error('至少选中一个工艺价格 !');
               return;
           }
           var postIds = [];
           var selectedCraftPriceIds = [];
           for(var index in selected){ //循环选中项
               if(selected[index].craftPriceId){
                   //后台存在真是数据
                   postIds.push(selected[index].craftPriceId);
                   selectedCraftPriceIds.push(selected[index]);
               }else {
                   vm.kuaiyinCraftOptionData.splice($.inArray(selected[index], vm.kuaiyinCraftOptionData), 1)
                   //仅前台页面数据 直接删除即可
               }
           }

            var self = this;
           if(postIds.length > 0){
               // 后台ajax 请求删除
               this.$confirm('确定要删除已配价选项吗?', '提示', {
                   confirmButtonText: '确定',
                   cancelButtonText: '取消',
                   type: 'warning'
               }).then(() => {
                   $.ajax({
                       type: "POST",
                       url: baseURL + "goodsCraftPrice/quoteGoodsCraftPrice/deleteByIds",
                       contentType: "application/json",
                       data: JSON.stringify(postIds),
                       success: function(r){
                           if(r.code == 0){
                               self.$message({message: '删除成功 !' ,type: 'success'});
                               self.initData();
                           }else{
                               alert(r.msg);
                           }
                       }
                   });
               }).catch(() => {
                   this.$message({type: 'info', message: '已取消删除'});
               })
           }

        },

	}
});